import numpy as np
import matplotlib.pyplot as plt

# 在四面体内随机生成点
def generate_random_point_in_tetrahedron(v0, v1, v2, v3):
    while True:
        u1,u2,u3=np.random.random(3)
        if u1+u2> 1:
            continue
        if u1+u3> 1:
            continue
        if u2+u3> 1:
            continue
        if u1+u2+u3 > 1:
            continue
        break
    # 计算新点的坐标
    p = v0+ u1*(v1-v0) + u2*(v2-v0) + u3*(v3-v0)
    return p

def generate_random_point_in_triangle(v1, v2, v3):
    r1,r2=np.random.random(2)
    p=(1-np.sqrt(r1))*v1+(np.sqrt(r1)*(1-r2))*v2+(np.sqrt(r1)*r2)*v3
    return p

v1=np.array([1,0,0])
v2=np.array([0,0,0])
v3=np.array([0,1,0])
v4=np.array([0,0,1])
coordinates = []
for i in range(5000):
    coordinates.append(generate_random_point_in_tetrahedron(v1, v2, v3, v4))

coordinates = np.array(coordinates)
print(coordinates)

# 创建3D图形
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 绘制点云
ax.scatter(coordinates[:, 0], coordinates[:, 1], coordinates[:, 2], c='r', marker='.', label='BS')

# 设置坐标轴标签
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# 设置坐标轴比例尺相同
ax.axis('equal')

# 添加图例
ax.legend()

# 显示图形
plt.show()